package com.adapter;

import java.util.ArrayList;
import java.util.Date;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bean.Answer;
import com.bean.UserInfo;
import com.easemob.util.DateUtils;
import com.xike.kyt.R;
import com.xike.util.MyUtil;

public class AnswerAdapter extends BaseAdapter {
	Context context;
	ArrayList<Answer> answers = new java.util.ArrayList<Answer>();
	boolean showLoveNum = true;

	public AnswerAdapter(Context context, ArrayList<Answer> answers) {
		this.context = context;
		this.answers = answers;
	}

	public AnswerAdapter(Context context, ArrayList<Answer> answers, boolean showLoveNum) {
		this.context = context;
		this.answers = answers;
		this.showLoveNum = showLoveNum;
	}

	ViewHolder holder;

	class ViewHolder {
		ImageView img_userImg;
		TextView tv_userName, tv_summary, tv_createTime, tv_loveNum;
		LinearLayout layout_love;
	}

	@Override
	public int getCount() {
		return answers.size();
	}

	@Override
	public Answer getItem(int position) {
		return answers.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = View.inflate(context, R.layout.listitem_answer, null);
			holder.img_userImg = (ImageView) convertView.findViewById(R.id.user_img_image);
			holder.tv_userName = (TextView) convertView.findViewById(R.id.user_tv_name);
			holder.tv_summary = (com.mUI.MeasureTextView) convertView.findViewById(R.id.lvitem_answer_tv_summary);
			holder.tv_createTime = (TextView) convertView.findViewById(R.id.user_tv_desc);

			holder.layout_love = (LinearLayout) convertView.findViewById(R.id.lvitem_answer_layout_love);
			holder.tv_loveNum = (TextView) convertView.findViewById(R.id.lvitem_answer_loveNum);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		Answer answer = getItem(position);

		UserInfo user = answer.getUser();
		if (user != null) {
			holder.tv_userName.setTextColor(Color.parseColor("#596b94"));
			MyUtil.setTextViewText(holder.tv_userName, user.getShow_name(), 0, "未知用户");
			BitmapDrawable bd = (BitmapDrawable) context.getResources().getDrawable(R.drawable.default_user_avator);
			MyUtil.displayImageView(context, user.getAvator(), holder.img_userImg, bd.getBitmap(), false);
		}

		if (showLoveNum) {
			holder.layout_love.setVisibility(View.VISIBLE);
			MyUtil.setTextViewText(holder.tv_loveNum, String.valueOf(answer.getLove_num()), 0, "0");
		} else {
			holder.layout_love.setVisibility(View.INVISIBLE);
		}

		try {
			MyUtil.setTextViewText(holder.tv_createTime, DateUtils.getTimestampString(new Date(answer.getCreate_time())), View.INVISIBLE, "");
		} catch (Exception e) {
			holder.tv_createTime.setVisibility(View.GONE);
			e.printStackTrace();
		}
		MyUtil.setTextViewText(holder.tv_summary, answer.getContent(), View.GONE, "");

		return convertView;
	}

}
